<?php
/**
 * Created by PhpStorm.
 * User: 冯轲
 * Date: 2017-12-6
 * Time: 16:08
 */
namespace app\admin\controller;

use app\admin\AdminCommon;
use think\Controller;
use think\Db;
use think\Hook;
use think\Request;
use think\Session;
use think\Validate;
use code\Code;

class Roles extends AdminCommon
{
    public function getindex()
    {
        $roles=new \app\admin\model\Roles();
        $data=$roles->select();
        $this->assign('data',$data);
        return $this->fetch('roles/index');
    }
    //添加角色
    public function getAdd()
    {
        return $this->fetch('roles/create');
    }
    //添加操作
    public function postDoadd(Request $request){
        $req=$request->except(['__token__','action']);
        $re=\app\admin\model\Roles::create($req);
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '添加成功',
            ];

        }else{
            $data=[
                'status' => 1,
                'msg' => '添加失败',
            ];
        };
        //必须是json数据的返回
        return json($data);
    }
    //删除操作
    public function postDelete($id){
        $re=\app\admin\model\Roles::destroy($id);
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '删除成功',
            ];
        }else{
            $data=[
                'status' => 1,
                'msg' => '删除失败',
            ];
        };
        return json($data);
    }
    //编辑操作
    public function getEdit($id)
    {
        $admin=new \app\admin\model\Roles();
        $data=$admin->where('id',$id)->select();
        $this->assign('data',$data);
        return view('roles/edit');
    }
    //编辑提交
    public function postDoEdit(Request $request,$id){
        $result=$request->except(['__token__','action']);
        $admin=new \app\admin\model\Roles();

        $re=$admin->save($result,['id' => $id]);
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '更新成功',
            ];
        }else{
            $data=[
                'status' => 1,
                'msg' => '更新失败',
            ];
        };
        return json($data);
    }
    //节点分配
    public function getnodes($id){
        //得到现在的角色信息
        $roles=new \app\admin\model\Roles();
        $role=$roles->where('id',$id)->select();
        $this->assign('role',$role);
        //得到角色与节点之间的关联信息
        $data=Db::name('role_node')->where('rid',$id)->select();
        $nids[]='';
        foreach ($data as $v){
            $nids[]=$v['nid'];
        }
        $this->assign('nids',$nids);
        //得到所有的节点信息
        $nodes=new \app\admin\model\Nodes();
        $node=$nodes->select();
        $this->assign('node',$node);
        return $this->fetch('roles/nodes');
    }
    //角色节点的保存
    public function postNodes(Request $request){
        $res=($request->except(['__token__','action','id']));
        $rid=$res['rid'];
        $nid=$res['nid'];
        Db::startTrans();
        try{
            //先删除所有UID为$uid的元素
            Db::name('role_node')->where('rid',$rid)->delete();
            foreach ($nid as $key=>$v){
                $node['rid']=$rid;
                $node['nid']=$v;
                //进行数据的输入
                Db::name('role_node')->insert($node);
            }
            // 提交事务
            Db::commit();
            $data=[
                'status'=>0,
                'msg'=>'角色分配成功',
            ];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $data=[
                'status'=>1,
                'msg'=>'角色分配失败',
            ];
        }

        return json($data);



    }
    //显示
    public function postStart($id){
        //
        $admin=new \app\admin\model\Roles();
        $result=$admin->save([
            'status'  => '0',
        ],['id' => $id]);
        if($result){
            $data=
                [
                    'status'=>0,
                    'msg'=>'已启用',
                    'id'=>$id,
                ];
        }else{
            $data=
                [
                    'status'=>1,
                    'msg'=>'出现未知错误，请稍后重试',
                    'id'=>$id,
                ];
        }
        return json($data);
    }
    //隐藏
    public function postStop($id){
        //
        $admin=new \app\admin\model\Roles();
        $result=$admin->save([
            'status'  => '1',
        ],['id' => $id]);
        if($result){
            $data=
                [
                    'status'=>0,
                    'msg'=>'已停用',
                    'id'=>$id,
                ];
        }else{
            $data=
                [
                    'status'=>1,
                    'msg'=>'出现未知错误，请稍后重试',
                    'id'=>$id,
                ];
        }
        return json($data);
    }
}